package com.cool.service;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Executors;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections4.comparators.ComparatorChain;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import com.cool.dto.request.QueryRequest;
import com.cool.pojo.Man;
import com.github.liaochong.myexcel.core.DefaultStreamExcelBuilder;
import com.github.liaochong.myexcel.utils.FileExportUtil;

import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author bian
 * @date 2021-02-11
 **/
@Service
@Slf4j
public class ExportService {
    public void export() {
        try (DefaultStreamExcelBuilder<Man> streamExcelBuilder =
            DefaultStreamExcelBuilder.of(Man.class).threadPool(Executors.newFixedThreadPool(10)).start()) {
            streamExcelBuilder.asyncAppend(Man::getManList);
            Workbook workbook = streamExcelBuilder.build();
            String fileName = "C:/Users/bian/Desktop" + ".xlsx";
            File file = new File(fileName);
            FileExportUtil.export(workbook, file);
        } catch (IOException e) {
            log.error("export error, error message: {}", e.getMessage());
        }
    }

    public List<Man> buildMultiSortDataList(QueryRequest queryRequest) {
        List<Man> dataList = Man.getManList();
        ComparatorChain<Man> multiSort = new ComparatorChain<>();
        Field[] declaredFields = Man.class.getDeclaredFields();
        queryRequest.getSortList().forEach(v -> Arrays.stream(declaredFields).forEach(vv -> {
            if (Optional.ofNullable(v.getName()).map(s -> v.getName().equals(vv.getName())).orElse(false))
                if (String.class.equals(vv.getType()))
                    multiSort.addComparator(new BeanComparator<>(v.getName(), Comparator.nullsLast(String::compareTo)),
                            v.getOrder());
                else if (Boolean.class.equals(vv.getType()))
                    multiSort.addComparator(new BeanComparator<>(v.getName(), Comparator.nullsLast(Boolean::compareTo)),
                            v.getOrder());
                else if (Integer.class.equals(vv.getType()))
                    multiSort.addComparator(new BeanComparator<>(v.getName(), Comparator.nullsLast(Integer::compareTo)),
                            v.getOrder());
                else if (Date.class.equals(vv.getType()))
                    multiSort.addComparator(new BeanComparator<>(v.getName(), Comparator.nullsLast(Date::compareTo)),
                            v.getOrder());
                else if (Long.class.equals(vv.getType()))
                    multiSort.addComparator(new BeanComparator<>(v.getName(), Comparator.nullsLast(Long::compareTo)),
                            v.getOrder());
                else if (Double.class.equals(vv.getType()))
                    multiSort.addComparator(new BeanComparator<>(v.getName(), Comparator.nullsLast(Double::compareTo)),
                            v.getOrder());
                else if (Float.class.equals(vv.getType()))
                    multiSort.addComparator(new BeanComparator<>(v.getName(), Comparator.nullsLast(Float::compareTo)),
                            v.getOrder());
                else if (Character.class.equals(vv.getType())) multiSort.addComparator(
                        new BeanComparator<>(v.getName(), Comparator.nullsLast(Character::compareTo)), v.getOrder());
                else
                    multiSort.addComparator(new BeanComparator<>(v.getName()), v.getOrder());
        }));
        dataList.sort(multiSort);
        return dataList;
    }

    public HttpHeaders getHttpHeaders(File file) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition",
            "attachment; filename=" + URLUtil.encode(file.getName(), StandardCharsets.UTF_8));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Last-Modified", LocalDateTime.now().toString());
        headers.add("ETag", Long.toString(System.currentTimeMillis()));
        return headers;
    }
}
